<!DOCTYPE html>
<html>
  <head th:include="/common/head :: head"></head>
  <style>
		.check_box {
				width:20px;
				height:20px
		}
  </style>
  <body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">销售管理</a>
        <a><cite>销售计划</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="iconfont" style="line-height:30px">&#xe6aa;</i></a>
    </div>
    <div class="x-body" ng-app="listApp" ng-controller="listController">
	  <div class="layui-row">
	  <form class="layui-form layui-col-md12 x-so">
	      <div class="layui-inline">
	      	  <label>月份:</label>
		      <div class="layui-input-inline">
		        <input type="text" class="layui-input" id="month" name="month" placeholder="yyyy-MM">
		      </div>
	      </div>
     
	      <button class="layui-btn btn_search" lay-submit="" lay-filter="sreach">
						<i class="layui-icon">&#xe615;</i>
		  </button>
	
	 </form>
	  </div>
	      <xblock>
	        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
	        <button class="layui-btn" onclick="addPlan('添加计划','/plan/planAdd.shtml',455,500)"><i class="layui-icon"></i>添加</button>
	        <span class="x-right" style="line-height:40px" id="span_total"></span>
	      </xblock>
	      <table class="layui-table">
	        <thead>
	          <tr>
	          	<th>
	              <input type="checkbox" class="check_box" name="checkAll" id="checkAll" onclick="checkAll(this)" />
	            </th>
	            <th>ID</th>
	            <th>姓名</th>
	            <th>月份</th>
	            <th>状态</th>
	            <th>最后修改时间</th>
	            <th>操作人</th>
	            <th>待处理人</th>
				<th>操作</th>
	        </thead>
	        <tbody>
	          <tr ng-repeat="plan in planList">
	          	<td>
	              <input type="checkbox" name="checkedPlans" class="check_box childs_check" value="{{plan.id}}"/>
	            </td>
	            <td>{{plan.id}}</td>
	            <td>{{plan.userName}}</td>
	            <td>{{plan.month}}</td>
	            <td>
	            	<span title="审核" onclick="audit('审核','/plan/planAudit.shtml',455,510,this)"   planId="{{plan.id}}" class="layui-btn layui-btn-normal layui-btn-mini" ng-if="plan.status==1">未审核</span>
	            	<span  onclick="back()" class="layui-btn layui-btn-normal layui-btn-mini" ng-if="plan.status==2">已通过</span>
	            	<span  onclick="back()" class="layui-btn layui-btn-danger layui-btn-mini" ng-if="plan.status==3">未通过</span>
				</td>
	            <td>{{plan.updateDate}}</td>
	            <td>{{plan.createBy}}</td>
	            <td>{{plan.managerName}}</td>
	            <td class="td-manage">
	            	<a title="删除" onclick="delPlan(this)" href="javascript:;" planId="{{plan.id}}" userName="{{plan.userName}}"> 
	            	<i class="layui-icon">&#xe640;</i>
	            	</a>
	            	<a title="编辑" onclick="edit('编辑','/plan/planUpdate.shtml',455,510,this)" href="" planId="{{plan.id}}"> 
	            	<i class="layui-icon">&#xe642;</i>
					</a>
				</td>
	          </tr>
	        </tbody>
	      </table>		
		<div id="page"></div>
	</div>
  </body>
	<script type="text/javascript">
	//全局对象
	var _this = {size:5,current:1};
	angular.module("listApp",[]).controller("listController",function($scope){
		  _this.scope = $scope; 
  	})
	//搜索表单对象
	var formData={month:""};
	$(function(){
		//获取数据
		getData();
		//绑定事件
  	  	bindEvent();
  	
	})
	//年月选择器
	layui.use('laydate', function(){
	    var laydate = layui.laydate;
		laydate.render({
		    elem: '#month'
		    ,type: 'month'
		  });
	});
	//条件查询
	function bindEvent(){
		//查询按钮的绑定事件
		$(".btn_search").bind("click",function(){
			formData.month = $("#month").val();
			_this.current = 1;
			getData();
		})
	}
	//获取数据
	function getData(){
		$.post("/plan/getPlanList","pageNo="+_this.current+"&pageSize="+_this.size+"&month="+formData.month,function(result){
			if(result.data.pages!=0 && result.data.records.length==0){
				_this.current=result.data.pages;
				getData();
				return;
			}
			showData(result);
		})
			
	}
	//展示数据
	function showData(result){
		//展示
		layui.use(['laypage', 'layer'], function(){
			 laypage = layui.laypage;
			 laypage.render({   //设置基础参数
			  elem: 'page',        //指向存放分页的容器，值可以是容器ID、DOM对象
			  count: result.data.total,   //数据总数
			  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			  limits: [5,10, 20],    //每页条数的选择项
			  curr:_this.current,    //起始页
			  limit:_this.size,      //每页显示的条数
			  jump: function(obj,first){  //jump切换分页的回调
				  				//obj（当前分页的所有选项值）、first（是否首次，一般用于初始加载的判断）
				  if(!first){
					  _this.current = obj.curr;
					  _this.size = obj.limit;
					  getData();
				  }
			  }
			 });
		})
		//展示列表
		_this.scope.planList = result.data.records;
		_this.scope.$apply();
		//展示总条数
		$("#span_total").html("共有数据:"+result.data.total+"条");
	}
	//执行全选操作
	function checkAll(ele){
		$(".childs_check").prop("checked",$(ele).prop("checked"));
	}
	//批量删除按钮
	function delAll(){
		var ids ="";
		var checkIds =document.getElementsByName("checkedPlans");
		for( i in checkIds){
			if(checkIds[i].checked==true){
				ids += checkIds[i].value+",";
			}
		}
		if(ids==""){
			parent.layer.msg("至少选中一条记录");
			return;
		}
		ids = ids.substr(0,ids.length-1);
		layer.confirm("确定要删除吗？",function(index){
			deletePlans(ids);
		})
	}
	//删除操作
	function deletePlans(ids){
		$.post("/plan/deletePlans","planIds="+ids,function(result){
			if(result.status==0){
				layer.msg(result.msg,function(){
					icon:1;
					getData();
				})
			}else{
				layer,msg(result.msg,function(){
					icon:2;
				});
			}
		})
	}
	//单个删除
	function delPlan(ele){
		layer.confirm("你确定删除【"+$(ele).attr("userName")+"】吗",function(){
			var planId = $(ele).attr("planId");
			$.post("/plan/deletePlans","planIds="+planId,function(result){
				if(result.status==0){
					layer.msg(result.msg,function(){
						icon:1;
						getData();
					})
				}else{
					layer.msg(result.msg,function(){
						icon:2;
					})
				}
			})
		})
	}
	//编辑按钮
	function edit(title,url,w,h,ele){
		var planId = $(ele).attr("planId");
		sessionStorage.setItem("planId",planId);
		parent.layer.open({
			type: 2,
	        area: [w+'px', h +'px'],
	        fix: false, //不固定
	        maxmin: true,
	        shadeClose: true,
	        shade:0.4,
	        title: title,
	        content: url,
	        end:function(){
	        	getData();
	        }
		})
	}
	//添加按钮
	function addPlan(title,url,w,h){
		parent.layer.open({
			type:2,
			area:[w+'px',h+'px'],
			fix:false,
			maxmin:true,
			shadeClose:true,
			shade:0.4,
			title:title,
			content:url,
			end:function(){
				getData();
			}
		})
	}
	
	//审核按钮
	function audit(title,url,w,h,ele){
		var planId = $(ele).attr("planId");
		sessionStorage.setItem("planId",planId);
		parent.layer.open({
			type: 2,
	        area: [w+'px', h +'px'],
	        fix: false, //不固定
	        maxmin: true,
	        shadeClose: true,
	        shade:0.4,
	        title: title,
	        content: url,
	        end:function(){
	        	getData();
	        }
		})
	}
	function back(){
		layer.msg("已审核，不进行二次审核。");
	}
	</script>
</html>